package net.wofly.sale.order.validator;

import net.wofly.sale.order.domain.Order;
import net.wofly.sale.order.domain.OrderContent;
import net.wofly.sale.order.domain.ReturnSales;
import net.wofly.sale.order.domain.ReturnSalesContent;
import net.wofly.sale.order.dto.req.BanquetOrderReq;
import net.wofly.sale.order.dto.req.CommodityTmlDto;
import net.wofly.sale.order.dto.req.NoMoneyOrderReq;
import net.wofly.sale.order.dto.req.ReturnGoodsOrderReq;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

public class OrderValidator {
    
    /**
     * 换行符
     */
    private static String lineSeparator = System.getProperty("line.separator");
    
    public static String validateCustomerID(String customerID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(customerID)) {
            stringBuffer.append("客户ID不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateOrderID(String orderID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(orderID)) {
            stringBuffer.append("订单ID不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateReturnSalesID(String returnSalesID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(returnSalesID)) {
            stringBuffer.append("退货单ID（returnSalesID）不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateRoleID(String roleID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(roleID)) {
            stringBuffer.append("角色ID不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateCreateOrderParams(Order order) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == order) {
            stringBuffer.append("订单对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(order.getBuyer())) {
                stringBuffer.append("订单的下单人不能为空");
            }
            if (order.getOrderType() == null || !net.wofly.sale.order.util.OrderType.isValueCorrect(
                    order.getOrderType())) {
                stringBuffer.append("订单类型不能为空或者值不正确").append(lineSeparator);
            }
            if (CollectionUtils.isEmpty(order.getOrderContents())) {
                stringBuffer.append("订单内容不能为空").append(lineSeparator);
            } else {
                for (OrderContent orderContent : order.getOrderContents()) {
                    if (StringUtils.isEmpty(orderContent.getCommodityChannelID())) {
                        stringBuffer.append("订单内容的商品渠道ID不能为空").append(lineSeparator);
                    }
                    if (null == orderContent.getNum() || orderContent.getNum() <= 0) {
                        stringBuffer.append("订单内容的商品数量不正确").append(lineSeparator);
                    }
                }
            }
            
            /*if (order.getGoodTransportType().intValue() == 1) {
                if (order.getOrderReceivingAddr() == null) {
                    stringBuffer.append("收货地址不能为空").append(lineSeparator);
                } else {
                    if (StringUtils.isEmpty(order.getOrderReceivingAddr().getReceivingAddrID())) {
                        stringBuffer.append("收货地址对象的ID不能为空").append(lineSeparator);
                    }
                }
            }*/
           /* if (order.getTakeGoodsShopInfo() == null) {
                stringBuffer.append("发货（提货）门店不能为空").append(lineSeparator);
            } else {
                if (StringUtils.isEmpty(order.getTakeGoodsShopInfo().getShopOrgID())) {
                    stringBuffer.append("发货（提货）门店对象的ID不能为空").append(lineSeparator);
                }
            }*/
        }
        return stringBuffer.toString();
    }
    
    public static String validateUpdateNoMoneyOrderParams(Order order) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == order) {
            stringBuffer.append("订单对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(order.getOrderID())) {
                stringBuffer.append("订单的下单人不能为空");
            }
            if (StringUtils.isEmpty(order.getBuyer())) {
                stringBuffer.append("订单的下单人不能为空");
            }
            if (CollectionUtils.isEmpty(order.getOrderContents())) {
                stringBuffer.append("订单内容不能为空").append(lineSeparator);
            } else {
                for (OrderContent orderContent : order.getOrderContents()) {
                    if (StringUtils.isEmpty(orderContent.getCommodityTerminalID())) {
                        stringBuffer.append("订单内容的商品终端ID不能为空").append(lineSeparator);
                    }
                    if (null == orderContent.getNum() || orderContent.getNum() <= 0) {
                        stringBuffer.append("订单内容的商品数量不正确").append(lineSeparator);
                    }
                }
            }
            
        }
        return stringBuffer.toString();
    }
    
    public static String validateChangeOrderTypeParams(Integer orderType, Order order) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (orderType == null || !net.wofly.sale.order.util.OrderType.isValueCorrect(orderType)) {
            stringBuffer.append("订单类型不能为空或者值不正确").append(lineSeparator);
        }
        
        if (null == order) {
            stringBuffer.append("订单对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(order.getBuyer())) {
                stringBuffer.append("订单的下单人不能为空");
            }
            if (order.getOrderType() == null || !net.wofly.sale.order.util.OrderType.isValueCorrect(
                    order.getOrderType())) {
                stringBuffer.append("订单类型不能为空或者值不正确").append(lineSeparator);
            }
            
            if (CollectionUtils.isEmpty(order.getOrderContents())) {
                stringBuffer.append("订单内容不能为空").append(lineSeparator);
            } else {
                for (OrderContent orderContent : order.getOrderContents()) {
                    if (null == orderContent.getCommodity()) {
                        stringBuffer.append("订单内容的商品对象信息不能为空").append(lineSeparator);
                    }
                    if (null == orderContent.getNum() || orderContent.getNum() <= 0) {
                        stringBuffer.append("订单内容的商品数量不正确").append(lineSeparator);
                    }
                }
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateEmployeeID(String employeeID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(employeeID)) {
            stringBuffer.append("员工ID不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateGetVerifyOrderParams(String userID, String order) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(userID)) {
            stringBuffer.append("用户ID不能为空！").append(lineSeparator);
        }
        
        if (StringUtils.isEmpty(order)) {
            stringBuffer.append("订单信息不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateStartWorkFlow(String userID, String orderID, String orderStatus) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(userID)) {
            stringBuffer.append("用户ID不能为空！").append(lineSeparator);
        }
        if (StringUtils.isEmpty(orderID)) {
            stringBuffer.append("订单ID不能为空！").append(lineSeparator);
        }
        if (StringUtils.isEmpty(orderStatus)) {
            stringBuffer.append("用户状态不能为空！").append(lineSeparator);
        }
        return stringBuffer.toString();
    }
    
    public static String validateUserID(String userID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(userID)) {
            stringBuffer.append("用户ID不能为空！").append(lineSeparator);
        }
        
        return stringBuffer.toString();
    }
    
    public static String complete(String userID, String taskID) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isEmpty(userID)) {
            stringBuffer.append("用户ID不能为空！").append(lineSeparator);
        }
        if (StringUtils.isEmpty(taskID)) {
            stringBuffer.append("任务ID不能为空！").append(lineSeparator);
        }
        
        return stringBuffer.toString();
    }
    
    public static String validateCreateOrderForReturnGoodsParams(ReturnGoodsOrderReq returnGoodsOrderReq) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == returnGoodsOrderReq) {
            stringBuffer.append("还货订单对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(returnGoodsOrderReq.getBuyerID())) {
                stringBuffer.append("订单的下单人不能为空");
            }
            if (StringUtils.isEmpty(returnGoodsOrderReq.getCustomerUserID())) {
                stringBuffer.append("客户ID不能为空");
            }
            if (CollectionUtils.isEmpty(returnGoodsOrderReq.getReturnCommodityTmlDtos())) {
                stringBuffer.append("还货商品列表不能为空").append(lineSeparator);
            } else {
                for (CommodityTmlDto tmlDto : returnGoodsOrderReq.getReturnCommodityTmlDtos()) {
                    if (StringUtils.isEmpty(tmlDto.getCommodityTmlID())) {
                        stringBuffer.append("还货商品列表商品终端ID不能为空").append(lineSeparator);
                    }
                    if (null == tmlDto.getNum() || tmlDto.getNum() <= 0) {
                        stringBuffer.append("货商品列表的商品数量不正确").append(lineSeparator);
                    }
                }
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateCreateNoMoneyOrderarams(NoMoneyOrderReq noMoneyOrderReq) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == noMoneyOrderReq) {
            stringBuffer.append("订单对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(noMoneyOrderReq.getBuyerID())) {
                stringBuffer.append("订单的下单人不能为空");
            }
            if (CollectionUtils.isEmpty(noMoneyOrderReq.getCommodityTmlDtos())) {
                stringBuffer.append("订单商品列表不能为空").append(lineSeparator);
            } else {
                for (CommodityTmlDto tmlDto : noMoneyOrderReq.getCommodityTmlDtos()) {
                    if (StringUtils.isEmpty(tmlDto.getCommodityTmlID())) {
                        stringBuffer.append("订单商品列表商品终端ID不能为空").append(lineSeparator);
                    }
                    if (null == tmlDto.getNum() || tmlDto.getNum() <= 0) {
                        stringBuffer.append("货商品列表的商品数量不正确").append(lineSeparator);
                    }
                }
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateCreateBanquetOrderarams(BanquetOrderReq banquetOrderReq) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == banquetOrderReq) {
            stringBuffer.append("订单对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(banquetOrderReq.getActivityID())) {
                stringBuffer.append("活动ID不能为空");
            }
            if (banquetOrderReq.getNum() == null) {
                stringBuffer.append("下单组数（数量）不能为空");
            }
        }
        return stringBuffer.toString();
    }
    
    public static String validateReturnSalesParams(String orderID, ReturnSales returnSales) {
        String err = validateOrderID(orderID);
        if (err.length() > 0) {
            return err;
        }
        return validateReturnSalesParams(returnSales);
    }
    
    
    public static String validateReturnSalesParams(ReturnSales returnSales) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == returnSales) {
            stringBuffer.append("退货对象不能为空").append(lineSeparator);
        } else {
            validateReturnSalesContent(returnSales, stringBuffer);
        }
        return stringBuffer.toString();
    }
    
    public static String validateEditAndReturnSalesParams(ReturnSales returnSales) {
        StringBuffer stringBuffer = new StringBuffer();
        
        if (null == returnSales) {
            stringBuffer.append("退货对象不能为空").append(lineSeparator);
        } else {
            if (StringUtils.isEmpty(returnSales.getReturnSalesID())) {
                stringBuffer.append("退货对象ID(returnSalesID)不能为空").append(lineSeparator);
            }
            validateReturnSalesContent(returnSales, stringBuffer);
            
        }
        return stringBuffer.toString();
    }
    
    private static void validateReturnSalesContent(ReturnSales returnSales, StringBuffer stringBuffer) {
        if (CollectionUtils.isEmpty(returnSales.getReturnSalesContents())) {
            stringBuffer.append("退货商品列表不能为空").append(lineSeparator);
        } else {
            for (ReturnSalesContent content : returnSales.getReturnSalesContents()) {
                if (StringUtils.isEmpty(content.getCommodityChannelID())) {
                    stringBuffer.append("退货商品列表商品渠道ID不能为空").append(lineSeparator);
                }
                if (StringUtils.isEmpty(content.getOrderContentID())) {
                    stringBuffer.append("退货商品列表中原订单商品内容ID（orderContentID）不能为空").append(lineSeparator);
                }
                if (null == content.getNum() || content.getNum() < 0) {
                    stringBuffer.append("退货商品列表的商品数量不正确").append(lineSeparator);
                }
            }
        }
    }
    
}
